﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JPushAPI.common;
using JPushAPI.common.resp;
using JPushAPI.device;
using JPushAPI.push;
using JPushAPI.push.mode;
using JPushAPI.push.notification;
using JPushAPI.report;
using JPushAPI.util;
using Newtonsoft.Json;
namespace JPushAPI
{
    public class JPush
    {
        static JPushClient client = new JPushClient("30f082262eb9a33b487befa8", "93488a7d7e8af00d60f3abf8");
        public static bool Push(string msg , params String[] ids)
        {
            PushPayload pushPayload = new PushPayload();
            pushPayload.platform = Platform.all();
            pushPayload.audience = Audience.s_alias(ids);
            Options option = new Options(); //设置生产环境可收到消息
            option.apns_production = true;
            option.time_to_live = 0;
            pushPayload.options = option;
            pushPayload.notification = new Notification().setAlert("郭大侠好");
            pushPayload.message = Message.content(msg);
            try
            {
                var result = client.SendPush(pushPayload);
                return true;
                //由于统计数据并非非是即时的,所以等待一小段时间再执行下面的获取结果方法
                //System.Threading.Thread.Sleep(1000);
                /*如需查询上次推送结果执行下面的代码*/
                //var apiResult = client.getReceivedApi(result.msg_id.ToString());
                // var apiResultv3 = client.getReceivedApi_v3(result.msg_id.ToString());
                /*如需查询某个messageid的推送结果执行下面的代码*/
                //var queryResultWithV2 = client.getReceivedApi("543f7cde56120d10bc4b58f2");
                //var querResultWithV3 = client.getReceivedApi_v3("543f7cde56120d10bc4b58f2");
            }
            catch (APIRequestException ex)
            {
                return false;
            }
        }
        

        public static bool Notify(string title, string context, params String[] uids)
        {
            PushPayload pushPayload = new PushPayload();
            pushPayload.platform = Platform.all();
            pushPayload.audience = Audience.s_alias(uids);
            Options option = new Options();
            option.apns_production = true;
            pushPayload.options = option;
            pushPayload.notification = new Notification().setAlert(context).setIos(new IosNotification().setContentAvailable(true).setSound("notification.wav").AddExtra("title", title).AddExtra("flag", "订单")).setAndroid(new AndroidNotification().AddExtra("title", title).AddExtra("flag", "订单"));
            try
            {
                var result = client.SendPush(pushPayload);
                return true;
                //由于统计数据并非非是即时的,所以等待一小段时间再执行下面的获取结果方法
                //System.Threading.Thread.Sleep(1000);
                /*如需查询上次推送结果执行下面的代码*/
                //var apiResult = client.getReceivedApi(result.msg_id.ToString());
                // var apiResultv3 = client.getReceivedApi_v3(result.msg_id.ToString());
                /*如需查询某个messageid的推送结果执行下面的代码*/
                //var queryResultWithV2 = client.getReceivedApi("543f7cde56120d10bc4b58f2");
                //var querResultWithV3 = client.getReceivedApi_v3("543f7cde56120d10bc4b58f2");

            }
            catch (APIRequestException ex)
            {
                return false;
            }
        }
        public static bool Notify2(string title, int id, string flag, params String[] uids)
        {
            PushPayload pushPayload = new PushPayload();
            pushPayload.platform = Platform.all();
            pushPayload.audience = Audience.s_alias(uids);
            Options option = new Options();
            option.apns_production = true;
            pushPayload.options = option;
            pushPayload.notification = new Notification().setAlert(title).setIos(new IosNotification().setSound("notification.wav").AddExtra("id", id).AddExtra("flag", flag)).setAndroid(new AndroidNotification().AddExtra("id", id).AddExtra("flag", flag));
            try
            {
                var result = client.SendPush(pushPayload);
                Common.Commons.Log(result.sendno+"**"+result.ResponseResult);
                return true;
                //由于统计数据并非非是即时的,所以等待一小段时间再执行下面的获取结果方法
                //System.Threading.Thread.Sleep(1000);
                /*如需查询上次推送结果执行下面的代码*/
                //var apiResult = client.getReceivedApi(result.msg_id.ToString());
                // var apiResultv3 = client.getReceivedApi_v3(result.msg_id.ToString());
                /*如需查询某个messageid的推送结果执行下面的代码*/
                //var queryResultWithV2 = client.getReceivedApi("543f7cde56120d10bc4b58f2");
                //var querResultWithV3 = client.getReceivedApi_v3("543f7cde56120d10bc4b58f2");

            }
            catch (APIRequestException ex)
            {
                return false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="title"></param>
        /// <param name="id"></param>
        /// <param name="flag"></param>
        /// <param name="count"></param>
        /// <param name="ringtype">响铃方式  1.响铃30秒  2.响铃1次  3.只推送不响铃</param>
        /// <param name="uids"></param>
        /// <returns></returns>
        public static bool Notify3(string title, int id, string flag, int count, int ringtype, params String[] uids)
        {
            PushPayload pushPayload = new PushPayload();
            pushPayload.platform = Platform.all();
            pushPayload.audience = Audience.s_alias(uids);
            Options option = new Options();
            option.apns_production = true;
            pushPayload.options = option;
            pushPayload.notification = new Notification().setAlert(title).setIos(new IosNotification().setContentAvailable(true).setSound("notification.wav").AddExtra("id", id).AddExtra("flag", flag).AddExtra("count", count).AddExtra("ringtype", ringtype)).setAndroid(new AndroidNotification().AddExtra("id", id).AddExtra("flag", flag).AddExtra("count", count).AddExtra("ringtype", ringtype));
            try
            {
                var result = client.SendPush(pushPayload);
                return true;
                //由于统计数据并非非是即时的,所以等待一小段时间再执行下面的获取结果方法
                //System.Threading.Thread.Sleep(1000);
                /*如需查询上次推送结果执行下面的代码*/
                //var apiResult = client.getReceivedApi(result.msg_id.ToString());
                // var apiResultv3 = client.getReceivedApi_v3(result.msg_id.ToString());
                /*如需查询某个messageid的推送结果执行下面的代码*/
                //var queryResultWithV2 = client.getReceivedApi("543f7cde56120d10bc4b58f2");
                //var querResultWithV3 = client.getReceivedApi_v3("543f7cde56120d10bc4b58f2");
            }
            catch (APIRequestException ex)
            {
                return false;
            }
        }
        public static bool Notify(string msg, string id, string table, params String[] uids)
        {
            PushPayload pushPayload = new PushPayload();
            pushPayload.platform = Platform.all();
            pushPayload.audience = Audience.s_alias(uids);
            Options option = new Options();
            option.apns_production = true;
            pushPayload.options = option;
            pushPayload.notification = new Notification().setAlert(msg).setIos(new IosNotification().setSound("notification.wav").AddExtra("table", table).AddExtra("id", id).AddExtra("flag", "圈子回复")).setAndroid(new AndroidNotification().AddExtra("table", table).AddExtra("id", id).AddExtra("flag", "圈子回复"));
            try
            {
                var result = client.SendPush(pushPayload);
                return true;
                //由于统计数据并非非是即时的,所以等待一小段时间再执行下面的获取结果方法
                //System.Threading.Thread.Sleep(1000);
                /*如需查询上次推送结果执行下面的代码*/
                //var apiResult = client.getReceivedApi(result.msg_id.ToString());
                // var apiResultv3 = client.getReceivedApi_v3(result.msg_id.ToString());
                /*如需查询某个messageid的推送结果执行下面的代码*/
                //var queryResultWithV2 = client.getReceivedApi("543f7cde56120d10bc4b58f2");
                //var querResultWithV3 = client.getReceivedApi_v3("543f7cde56120d10bc4b58f2");

            }
            catch (APIRequestException ex)
            {
                return false;
            }
        }

        public static bool Clear(params String[] ids)
        {

            PushPayload pushPayload = new PushPayload();
            pushPayload.platform = Platform.all();
            pushPayload.audience = Audience.s_alias(ids);
            Options option = new Options();
            option.apns_production = true;
            pushPayload.options = option;
            pushPayload.notification = new Notification().setAlert("").setIos(new IosNotification().setBadge(0));
            try
            {
                var result = client.SendPush(pushPayload);
                return true;
                //由于统计数据并非非是即时的,所以等待一小段时间再执行下面的获取结果方法
                //System.Threading.Thread.Sleep(1000);
                /*如需查询上次推送结果执行下面的代码*/
                //var apiResult = client.getReceivedApi(result.msg_id.ToString());
                // var apiResultv3 = client.getReceivedApi_v3(result.msg_id.ToString());
                /*如需查询某个messageid的推送结果执行下面的代码*/
                //var queryResultWithV2 = client.getReceivedApi("543f7cde56120d10bc4b58f2");
                //var querResultWithV3 = client.getReceivedApi_v3("543f7cde56120d10bc4b58f2");

            }
            catch (APIRequestException ex)
            {
                return false;
            }
        }

    }
}
